#!/bin/bash

XDIR_parent=$(cd $(dirname $0); cd ..; pwd)

source $XDIR_parent/install_env.conf
. $XDIR_parent/util/func.sh

# 停止tomcat
function shutdown_web()
{
  kill_proc "HYRENSERVWEB"
  echo_info "shutdown webserver ... ... done."
}

# 启动tomcat
function startup_web()
{
  echo_info "start webserver ... ..."
  $HRS_TOMCAT_HOME/bin/startup.sh
  #waiting_proc_status $HRS_TOMCAT_PORT 0 "web start"
  sleep 1
  local nums=0
  while true; do
    # 有这一行表明已经启动成功
    grep "Server startup in [0-9]* ms" $HRS_TOMCAT_LOGSDIR/catalina.out > /dev/null
    if [ $? -eq 0 ]; then
      break;
    fi
    nums=$(expr $nums + 1 )
    if [ $nums -gt 600 ]; then
      die_insh "start webserver Fail! check logfile : $HRS_TOMCAT_LOGSDIR/catalina.out"
    else
      sleep 1
      echo_doing "." "no indent"
    fi
  done
  netstat -tpnl | grep -w $HRS_TOMCAT_PORT > /dev/null 2>&1 || { die_insh "no webserver port [$HRS_TOMCAT_PORT]! check logfile : $HRS_TOMCAT_LOGSDIR/catalina.out"; }
  echo_info "start webserver ... ... done."
  grep ".*Exception:" $HRS_TOMCAT_LOGSDIR/catalina.out > /dev/null
  if [ $? -eq 0 ]; then
    echo_warn "Find Exception in $HRS_TOMCAT_LOGSDIR/catalina.out, please check it carefully! "
    confirm_operate "Will be stop this installation!"
    if [ $? -eq 0 ]; then
      die_insh
    fi
  fi
}
# 重启tomcat
function restart_web()
{
  shutdown_web
  echo ""
  startup_web
}

  # 部署war包
  value_arr=($HRS_DEPLOYED_WARS)
  echo_info "Deploy wars ... ..."
  for warName in ${value_arr[@]}; do
    cp -rf "$HYREN_USERPD_WARS/${warName}.war" $HRS_TOMCAT_HOME/webapps || { die_insh "copy $HYREN_USERPD_WARS/${warName}.war fail"; }
  done
  unset warName
  echo_info "Deploy wars ... ... DONE."

  startup_web

  echo_info "Configure warapp properties ... ..."
  echo_info "location is : $HRS_TOMCAT_HOME/webapps"
  for warName in ${value_arr[@]}; do
    #warName=$(echo "$warFile" | sed "s/\..*//")
    echo_doing "config ${warName} ... ..."
    nums=0
    while [ ! -f "$HRS_TOMCAT_HOME/webapps/${warName}/WEB-INF/classes/log4j.properties" ]; do
      nums=$(expr $nums + 1 )
      if [ $nums -gt 600 ]; then
        echo_error "Deploy war [${warName}] fail, please solved it manually."
        break
      else
        sleep 1
        echo_doing "." "no indent"
      fi
    done
    if [ $nums -gt 600 ]; then
      continue
    fi
    sleep 1
    sed -i "s%^log4j\.appender\.DailyFile\.File.*%log4j\.appender\.DailyFile\.File=$HRS_TOMCAT_LOGSDIR\/$warName\.log%g" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/log4j.properties || { die_insh "sed log4j DailyFile fail"; }
    grep -w "$HRS_TOMCAT_LOGSDIR/$warName.log" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/log4j.properties > /dev/null || { die_insh "seded DailyFile.File value fail in log4j"; }
    #echo "sed 2 result : $?"
    # dbinfo : pgsql
    sed -i "s/^conn_url=jdbc:postgresql:.*/conn_url=jdbc:postgresql:\/\/$HRS_PGSQL_HOST:$HRS_PGSQL_PORT\/$HRS_PGSQL_DBNAME/" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : sed conn_url fail"; }
    grep -w "conn_url=jdbc:postgresql://$HRS_PGSQL_HOST:$HRS_PGSQL_PORT/$HRS_PGSQL_DBNAME" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded conn_url value fail in dbinfo"; }

    sed -i "s/^dbuser=.*/dbuser=$HRS_PGSQL_DBUSER/" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : sed dbuser fail"; }
    grep -w "dbuser=$HRS_PGSQL_DBUSER" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded dbuser value fail in dbinfo"; }

    sed -i "s/^dbpasswd=.*/dbpasswd=$HRS_PGSQL_DBPAWD/" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : sed dbpasswd fail"; }
    grep -w "dbpasswd=$HRS_PGSQL_DBPAWD" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded dbpasswd value fail in dbinfo"; }

    # dbinfo : hive
    sed -i "s/conn_url_hive.*/conn_url_hive=jdbc:hive2:\/\/$HIVE_HOST:$HIVE_PORT\/$HIVE_DATABASE_NAME/g" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : sed conn_url_hive fail!"; }
    grep -w "conn_url_hive=jdbc:hive2://$HIVE_HOST:$HIVE_PORT/$HIVE_DATABASE_NAME" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded conn_url_hive value fail in dbinfo"; }

    sed -i "s/dbuser_hive.*/dbuser_hive=$HYREN_USER/g" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : sed dbuser_hive fail!"; }
    grep -w "dbuser_hive=$HYREN_USER" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded dbuser_hive value fail in dbinfo"; }
    
    # dbinfo : phoenix
    value_arr=($HRS_ZOOKEEPER_HOSTS)
    for i in ${value_arr[@]}; do
      j=$j,$i
    done
    j=${j:1}
    sed -i "s/conn_url_phoenix.*/conn_url_phoenix=jdbc:phoenix:$j/g" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : sed phoenix fail!"; }
    grep -w "conn_url_phoenix=jdbc:phoenix:$j" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded conn_url_phoenix value fail in dbinfo"; }

    # dbinfo : spark-thrift
    sed -i "s/conn_url_spark.*/conn_url_spark=jdbc:hive2:\/\/$HRS_SPARKTHRIFT_HOST:$HRS_SPARKTHRIFT_PORT\/$HYREN_USER/" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : conn_url_spark fail!"; }
    grep -w "conn_url_spark=jdbc:hive2://$HRS_SPARKTHRIFT_HOST:$HRS_SPARKTHRIFT_PORT/$HYREN_USER" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded conn_url_spark value fail in dbinfo"; }

    sed -i "s/dbuser_spark.*/dbuser_spark=$HYREN_USER/g" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "${warName} : dbinfo : dbuser_spark fail!"; }
    grep -w "conn_url_spark=jdbc:hive2://$HRS_SPARKTHRIFT_HOST:$HRS_SPARKTHRIFT_PORT/$HYREN_USER" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded conn_url_spark value fail in dbinfo"; }

    # dbinfo : 如果gp未安装 此处填写的信息应该是pgsql的主机、端口等
    sed -i "s/conn_url_gp.*/conn_url_gp=jdbc:postgresql:\/\/$HRS_GP_HOST:$HRS_GP_PORT\/$HRS_GP_DBNAME/" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : conn_url_gp fail!"; }
    grep -w "conn_url_gp=jdbc:postgresql:\/\/$HRS_GP_HOST:$HRS_GP_PORT\/$HRS_GP_DBNAME" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded conn_url_gp value fail in dbinfo"; }

    sed -i "s/dbuser_gp.*/dbuser_gp=$HRS_GP_DBUSER/" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : dbuser_gp fail!"; }
    grep -w "dbuser_gp=$HRS_GP_DBUSER" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded dbuser_gp value fail in dbinfo"; }

    sed -i "s/dbpasswd_gp.*/dbpasswd_gp=$HRS_GP_DBPAWD/" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties || { die_insh "dbinfo : dbpasswd_gp fail!"; }
    grep -w "dbpasswd_gp=$HRS_GP_DBPAWD" $HRS_TOMCAT_HOME/webapps/$warName/WEB-INF/classes/dbinfo.properties > /dev/null || { die_insh "seded dbpasswd_gp value fail in dbinfo"; }

    echo_done
  done
  echo_info "Configure warapp properties ... ... DONE."

  unset value_arr
  unset warName
  unset nums

  shutdown_web
  sleep 3
  #startup_web
